package com.qf.controller;

import com.qf.pojo.*;
import com.qf.service.AddressService;
import com.qf.service.OrderService;
import com.qf.service.OrderdetailService;
import com.qf.service.impl.AddressServiceImpl;
import com.qf.service.impl.OrderServiceImpl;
import com.qf.service.impl.OrderdetailServiceImpl;
import com.qf.utils.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/*
 *@Description:
 *@author:刘哥
 *@Date:86180 2022/8/16 22:21
 */
@WebServlet("/order")
public class OrderServlet extends BaseServlet{
    private  AddressService addressService=new AddressServiceImpl();
    private OrderService orderService=new OrderServiceImpl();
    private OrderdetailService orderdetailService=new OrderdetailServiceImpl();
//    /order?method=preView
    public String preView(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        TbUser user = (TbUser) request.getSession().getAttribute("user");
        if (user == null) {
            return "/login.jsp";
        }
        Long userId = user.getId();
        List<TbAddress> address = addressService.getAddress(userId);
        request.setAttribute("addressList",address);

        return "/order.jsp";
    }
//    order?method=create
    public String create(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        TbUser user = (TbUser) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        String aid = request.getParameter("aid");
        if(StringUtil.isEmpty(aid)){
            return "redirect:/order?method=preView";
        }
        List<TbCart>  cart = (List<TbCart>) request.getSession().getAttribute("cart");
//       返回的是订单id和价格
        Map result=orderService.creatOrder(user.getId(),Long.parseLong(aid),cart);

        //下单完成后session中的购物车数据也要清空
        //加速垃圾回收
       request.getSession().removeAttribute("cart");
       request.getSession().setAttribute("order",result);

        return "redirect:/orderSuccess.jsp";
    }
//    order?method=show
    public String show(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        TbUser user = (TbUser) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        List<TbOrder> ordersList =orderService.findOrder(user.getId());

        request.setAttribute("ordersList", ordersList);

        return "/orderList.jsp";
    }
//    /order?method=detail
    public String detail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        TbUser user = (TbUser) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        String orderId = request.getParameter("id");
        TbOrder order = orderService.selectOrder(orderId);
//        System.out.println("order = " + order);

        request.setAttribute("order",order);

        return "/orderDetail.jsp";
    }
}
